import { defineConfig } from "vitepress";
import path from "node:path";
import fs from "node:fs";

const createWithTextFunction = (rootPath: string) => {
  return (filePath: string = "") => {
    const relativePath = "./" + rootPath + filePath;
    let absolutePath = path.resolve(relativePath);
    let text = "";
    if (filePath === "") {
      absolutePath += "/index.md";
    } else {
      absolutePath += ".md";
    }
    if (fs.existsSync(absolutePath)) {
      const content = fs.readFileSync(absolutePath, { encoding: "utf-8" });
      if (content.length > 0) {
        const result = content.match(/#\ (.*?)\r\n/);
        if (result !== null) {
          text = result[1];
        }
      }
    }
    return {
      text: text,
      link: rootPath + filePath,
    };
  };
};

const senior = createWithTextFunction("/senior/");

// https://vitepress.dev/reference/site-config
export default defineConfig({
  title: "Study Math",
  description: "I am starting to study math.",
  base: "/study-math/",
  themeConfig: {
    // https://vitepress.dev/reference/default-theme-config
    nav: [
      { text: "初中数学", link: "/basic/" },
      { text: "高中数学", link: "/advanced/" },
      { text: "高等数学", link: "/senior/" },
    ],

    sidebar: {
      "/advanced/": [{ text: "首页", link: "/advanced/" }],
      "/senior/": [
        senior("multivariate-function"),
        { text: "空间直角坐标系", link: "/senior/coordinate" },
        { text: "重积分", link: "/senior/" },
        { text: "二重积分", link: "/senior/double-integral" },
      ],
    },

    socialLinks: [
      { icon: "github", link: "https://github.com/vuejs/vitepress" },
    ],
  },
});
